package com.buyantech.smweb.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseSpringController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;

import cn.org.rapid_framework.page.Page;

import com.buyantech.smweb.excel.ExcelAVieData;
import com.buyantech.smweb.model.ExchangeType;
import com.buyantech.smweb.model.Types;
import com.buyantech.smweb.model.User;
import com.buyantech.smweb.service.ExchangeTypeManager;
import com.buyantech.smweb.service.TypesManager;
import com.buyantech.smweb.vo.query.ExchangeTypeQuery;
import com.google.gson.Gson;

/**
 * 积分兑换产品配置
 * @version 1.0
 * @date 2012-05-10
 */

@Controller
public class ExchangeTypeController extends BaseSpringController {

	private ExchangeTypeManager exchangeTypeManager;
	
	private TypesManager typesManager;
	
	private final String LIST_ACTION = "redirect:/pages/User/list.do";
 
	public void setExchangeTypeManager(ExchangeTypeManager exchangeTypeManager) {
		this.exchangeTypeManager = exchangeTypeManager;
	}

	public void setTypesManager(TypesManager typesManager) {
		this.typesManager = typesManager;
	}
	
	public void init(ModelMap model) {
		model.put("now", new java.sql.Timestamp(System.currentTimeMillis()));
	}
	/** 
	 * 执行搜索 
	 **/
	public ModelAndView list(HttpServletRequest request,HttpServletResponse response,ExchangeTypeQuery query) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		
		query.setPageNumber(Integer.parseInt(request.getParameter("pageIndex"))+1);
		query.setPageSize(Integer.parseInt(request.getParameter("pageSize")));
		String sortfile=request.getParameter("sortField");
		if(sortfile!=null&&!sortfile.equals(""))
		  query.setSortColumns(request.getParameter("sortField")+" "+request.getParameter("sortOrder"));
		//else  query.setSortColumns("crdate"+" "+request.getParameter("des"));
		
		Page page = this.exchangeTypeManager.searchPage(query);
		modelMap.put("data", page.getResult());
		modelMap.put("total", page.getTotalCount());
		JSONObject obj = new JSONObject(modelMap);
		ajaxPack(obj.toString(), request, response);
		return null;
	}	
	
	
	/** 
	 * 执行搜索 
	 **/
	public ModelAndView typelist(HttpServletRequest request,HttpServletResponse response,ExchangeTypeQuery query) {
		List<Types> list=typesManager.findAll();
		StringBuffer sf=new StringBuffer("[");
		boolean isf=false;
		for(Types types:list){
			isf=true;
			sf.append("{\"id\":");
			sf.append("\""+types.getId()+"\"");
			sf.append(",\"text\":");
			sf.append("\""+types.getTypesStr()+"\"},");
		}
		String json=sf.toString();
		if(isf)json=json.substring(0,sf.length()-1);
		json+="]";
		System.out.println(json);
		ajaxPack(json, request, response);
		return null;
	}	
	
	 /** 
	 * 执行export 
	 **/
	public ModelAndView exportData(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView result = new ModelAndView(new ExcelAVieData());
		String type="";
		List<String>headers = new ArrayList<String>();
		headers.add("姓名");
		headers.add("时间");
		headers.add("类型");
		headers.add("积分"); 
		headers.add("金额"); 
		headers.add("状态");
 
		ExchangeTypeQuery query=new ExchangeTypeQuery();
		query.setPageSize(10000);
		Page page = this.exchangeTypeManager.searchPage(query);
		
		result.addObject("headers", headers);
		result.addObject("type", "1");
		result.addObject("content", page.getResult());
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-Desposition","attachment; filename=demo.xls");
		return result;
	}
	
	/**
	 * 登出
	 **/
	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response, User user) throws Exception {
		String id=request.getParameter("id");
		if(id.length()>0){
			id=id.substring(0,id.length()-1);
		}
		try {
			exchangeTypeManager.remove("DELETE FROM ExchangeType WHERE id in("+id+")");
			ajaxPack("{state:true}",request,response);
		} catch (RuntimeException e) {
			ajaxPack("{state:false}",request,response);
		}
		return null;
	}
	
	/**
	 * 根据id获取用户对象
	 **/
	public ModelAndView getId(HttpServletRequest request,
			HttpServletResponse response,ExchangeType exchangeType) throws Exception {
		String id=request.getParameter("id");
		exchangeType=exchangeTypeManager.getById(id);
		if(exchangeType!=null){
			JSONObject obj = new JSONObject(exchangeType);
			ajaxPack(obj.toString(), request, response);
		}
		return null;
	}
	
	/** 
	 * 保存新增对象
	 **/
	public ModelAndView save(HttpServletRequest request,HttpServletResponse response,ExchangeType exchangeType) throws Exception {
		String daTypeStr=request.getParameter("submitData");
		Gson gson=new Gson();
		exchangeType=gson.fromJson(daTypeStr, ExchangeType.class); 
		try{
			Types types=typesManager.getById(exchangeType.getTypeid());
			exchangeType.setTypename(types.getTypesStr());
			if(StringUtils.isNotEmpty(exchangeType.getId()))
				exchangeTypeManager.update(exchangeType);
			else 
				exchangeTypeManager.save(exchangeType);
		}catch (RuntimeException e) {
			ajaxPack("{state:false}",request,response);
			return null;
		}
		ajaxPack("{state:true}",request,response);
		return null;
	}
	
}
